
var app = getApp()
var ConstantData = require('../../../../common/constant_data.js')

function scan(funJump) {
  wx.scanCode({
    onlyFromCamera: true,
    success: (res) => {
      var qrc = '';
      var qrCode = res.result;
      if (!qrCode) {
        wx.showToast({
          title: '二维码为空',
        })
        return;
      }
      let macReg = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/;
      let isMacAddr = macReg.test(qrCode);
      if (qrCode.indexOf(ConstantData.CONSTATNT_DATA.DEVICE_QRCODE_URL_PREFIX) == 0 || isMacAddr) {
        //设备二维码
        let qrc_old = app.getScanQrcContent();
        if (isMacAddr) {
          qrc = qrCode.replace(/:/g, "");
        } else {
          var rst = qrCode.split('/');
          qrc = rst[rst.length - 1];
        }
        app.globalData.scan.qrc = qrc;
        try {
          wx.setStorageSync('scan_qrc', qrc);
        } catch (e) {}
        if (qrc != qrc_old) {
          wx.removeStorageSync('blue_fail_time')
          wx.removeStorageSync('rfid_error_time')
          wx.removeStorageSync('insert_fail_time')
          wx.removeStorageSync('hook_fail_time')
          wx.removeStorageSync('has_opened_gprs')
          wx.removeStorageSync('no_eject_times')
        }
      } else {
        wx.showToast({
          title: '二维码有误',
          icon: 'error',
          duration: 2000
        })
        return
      }
      // that.data.qrc = qrc;
      // that.getPileState()
      funJump(qrc)
    },
    fail: function (res) {
     
    }
  });

}

exports.scan = scan